#!/usr/bin/env python3

from launch import LaunchDescription
from launch_ros.actions import ComposableNodeContainer, Node
from launch_ros.descriptions import ComposableNode
from launch.actions import DeclareLaunchArgument, TimerAction
from launch.substitutions import LaunchConfiguration
from launch.conditions import IfCondition

def generate_launch_description():
    # 声明启动参数
    use_communication_arg = DeclareLaunchArgument(
        'use_communication',
        default_value='true',
        description='Whether to use component communication demo'
    )

    processing_delay_arg = DeclareLaunchArgument(
        'processing_delay_ms',
        default_value='100',
        description='Processing delay in milliseconds'
    )

    aggregation_window_arg = DeclareLaunchArgument(
        'aggregation_window_ms',
        default_value='2000',
        description='Aggregation window in milliseconds'
    )

    # 组件通信容器 - 展示组件间通信
    communication_container = ComposableNodeContainer(
        name='communication_component_container',
        namespace='',
        package='rclcpp_components',
        executable='component_container',
        composable_node_descriptions=[
            # 数据发布者
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentPublisher',
                name='data_publisher',
                parameters=[{
                    'publish_frequency': 5.0,
                    'message_prefix': 'Raw Data'
                }],
                remappings=[
                    ('component_topic', 'raw_data')
                ]
            ),

            # 数据处理器
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentProcessor',
                name='data_processor',
                parameters=[{
                    'processing_delay_ms': LaunchConfiguration('processing_delay_ms'),
                    'add_timestamp': True
                }]
            ),

            # 数据聚合器
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentAggregator',
                name='data_aggregator',
                parameters=[{
                    'aggregation_window_ms': LaunchConfiguration('aggregation_window_ms'),
                    'max_messages_per_batch': 5
                }]
            ),

            # 系统监控器
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentMonitor',
                name='system_monitor',
                parameters=[{
                    'monitoring_interval_ms': 3000,
                    'alert_threshold': 10
                }]
            ),

            # 最终消费者
            ComposableNode(
                package='multi_launch_demo',
                plugin='multi_launch_demo::ComponentSubscriber',
                name='final_consumer',
                parameters=[{
                    'node_name': 'final_consumer'
                }],
                remappings=[
                    ('component_topic', 'aggregated_data')
                ]
            )
        ],
        output='screen',
        condition=IfCondition(LaunchConfiguration('use_communication'))
    )

    # 延迟启动监控器 - 展示动态组件加载
    delayed_monitor = TimerAction(
        period=10.0,
        actions=[
            ComposableNodeContainer(
                name='delayed_monitor_container',
                namespace='',
                package='rclcpp_components',
                executable='component_container',
                composable_node_descriptions=[
                    ComposableNode(
                        package='multi_launch_demo',
                        plugin='multi_launch_demo::ComponentMonitor',
                        name='delayed_monitor',
                        parameters=[{
                            'monitoring_interval_ms': 2000,
                            'alert_threshold': 5
                        }]
                    )
                ],
                output='screen'
            )
        ],
        condition=IfCondition(LaunchConfiguration('use_communication'))
    )

    return LaunchDescription([
        use_communication_arg,
        processing_delay_arg,
        aggregation_window_arg,
        communication_container,
        delayed_monitor
    ])
